//
//  PublishCar.m
//  JiongTu
//
//  Created by Dupengfei on 16/3/29.
//  Copyright © 2016年 YSKJ. All rights reserved.
//

#import "PublishCar.h"

#import "JTDriverTrip.h"
#import "JTConst.h"
#import "JTUser.h"

#import <AVOSCloud.h>

@implementation PublishCar

+ (void)driver:(AVObject *)object publish:(JTDriverTrip *)trip {
    AVObject * oneDriver = [AVObject objectWithoutDataWithClassName:kJTClassDriver objectId:object.objectId];
    
    AVObject *driverTrip = [AVObject objectWithClassName:kJTClassJourneyDriver];
    [driverTrip setObject:trip.startLocation forKey:kJTJourneyKeyStart];
    [driverTrip setObject:trip.startStreet forKey:kJTJourneyKeyStartStreet];
    [driverTrip setObject:trip.endLocation forKey:kJTJourneyKeyEnd];
    [driverTrip setObject:trip.endStreet forKey:kJTJourneyKeyEndStreet];
    [driverTrip setObject:trip.time forKey:kJTJourneyKeyDate];
    [driverTrip setObject:trip.timeInterval forKey:kJTJourneyKeyTimeInterval];
    [driverTrip setObject:trip.remark forKey:kJTJourneyKeyRemark];
    [driverTrip setObject:trip.seatCount forKey:kJTJourneyKeySeats];
    [driverTrip setObject:trip.identity forKey:kJTJourneyKeyIdentity];
    [driverTrip setObject:trip.driver forKey:@"DriverInformation"];
    [driverTrip setObject:trip.price forKey:@"Price"];
    [driverTrip setObject:trip.start forKey:@"Start"];
    [driverTrip setObject:trip.end forKey:@"End"];
    AVObject * user =[AVObject objectWithoutDataWithClassName:@"_User" objectId:[AVUser currentUser].objectId];
    [user incrementKey:@"current"];
    [user save];
    [driverTrip saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {

            [[NSUserDefaults standardUserDefaults] setObject:driverTrip.objectId forKey:tmpDriverTripID];
            AVRelation *driverTripInfo = [oneDriver relationForKey:kJTDriverKeyInfo];
            [driverTripInfo addObject:driverTrip];
            
            [oneDriver saveInBackground];
        }
    }];
}

+ (void)firstPublish:(JTDriverTrip *)trip {
    AVObject * driver = [AVObject objectWithClassName:kJTClassDriver];
    [driver setObject:trip.driver.objectId forKey:kJTDriverKeyId];
    
    AVObject *driverTrip = [AVObject objectWithClassName:kJTClassJourneyDriver];
    [driverTrip setObject:trip.startLocation forKey:kJTJourneyKeyStart];
    [driverTrip setObject:trip.startStreet forKey:kJTJourneyKeyStartStreet];
    [driverTrip setObject:trip.endLocation forKey:kJTJourneyKeyEnd];
    [driverTrip setObject:trip.endStreet forKey:kJTJourneyKeyEndStreet];
    [driverTrip setObject:trip.time forKey:kJTJourneyKeyDate];
    [driverTrip setObject:trip.timeInterval forKey:kJTJourneyKeyTimeInterval];
    [driverTrip setObject:trip.remark forKey:kJTJourneyKeyRemark];
    [driverTrip setObject:trip.seatCount forKey:kJTJourneyKeySeats];
    [driverTrip setObject:trip.identity forKey:kJTJourneyKeyIdentity];
    [driverTrip setObject:trip.driver forKey:@"DriverInformation"];
    [driverTrip setObject:trip.price forKey:@"Price"];
    [driverTrip setObject:trip.start forKey:@"Start"];
    [driverTrip setObject:trip.end forKey:@"End"];
    AVObject * user =[AVObject objectWithoutDataWithClassName:@"_User" objectId:[AVUser currentUser].objectId];
    [user incrementKey:@"current"];
    [user save];
    [driverTrip saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            [[NSUserDefaults standardUserDefaults] setObject:driverTrip.objectId forKey:tmpDriverTripID];
            AVRelation *driverTripInfo = [driver relationForKey:kJTDriverKeyInfo];
            [driverTripInfo addObject:driverTrip];
            
            [driver saveInBackground];
        }
    }];
    
    
}

@end
